<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeWindow">
    <el-form ref="employeeForm" label-width="120px" :rules="rules" :model="employeeFormData">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="employeeFormData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="employeeFormData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="employeeFormData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" :editable="false" @change="checkTwoTimeData" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="employeeFormData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in employeesEnum.hireType" :key="item.id" :value="item.id" :label="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="employeeFormData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="employeeFormData.departmentName" style="width:50%" placeholder="请选择部门" @focus="getDepartments" />
        <div v-if="treeData.length > 0 && showTree" class="treeContainer">
          <el-tree :data="treeData" :default-expand-all="true" :props="{label:'name', children: 'children'}" class="treeDeparment" @node-click="selectNode" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="employeeFormData.correctionTime" style="width:50%" placeholder="请选择转正时间" :editable="false" @change="checkTwoTimeData" />
      </el-form-item>
    </el-form>
    <template>
      <el-row type="flex" justify="center" style="margin-top:50px">
        <el-col :span="6">
          <el-button type="primary" @click="ok">确定</el-button>
          <el-button @click="closeWindow">取消</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import employeesEnum from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'
// 将数据转为树型结构使用的数据结构
import { departmentsTreeData } from '@/utils'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const checkTime = (rule, value, callback) => {
      if (!this.employeeFormData.timeOfEntry || !this.employeeFormData.correctionTime) {
        callback()
      } else {
        const timeOfEntry = this.employeeFormData.timeOfEntry.getTime()
        const correctionTime = this.employeeFormData.correctionTime.getTime()
        timeOfEntry < correctionTime ? callback() : callback('入职前不能转正！')
      }
    }
    return {
      treeData: [],
      employeesEnum,
      showTree: false,
      employeeFormData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, trigger: 'blur', message: '用户姓名不能为空' },
          { min: 1, max: 10, message: '用户姓名为1-10位' }
        ],
        mobile: [
          { required: true, trigger: 'blur', message: '手机号不能为空' },
          { pattern: /^1[3-9]\d{9}$/, trigger: 'blur', message: '手机号格式不正确' }
        ],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' },
          { validator: checkTime, trigger: 'change' },
          { validator: checkTime, trigger: 'blur' }
        ],
        correctionTime: [
          { trigger: 'change', validator: checkTime },
          { trigger: 'blur', validator: checkTime }
        ]
      }
    }
  },
  methods: {
    async getDepartments() {
      if (this.treeData.length === 0) {
        const { depts } = await getDepartments()
        this.treeData = departmentsTreeData(depts, '')
      }

      this.showTree = true
    },
    selectNode(data, node, tree) {
      this.employeeFormData.departmentName = data.name
      this.showTree = false
    },
    closeWindow() {
      this.employeeForm = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.employeeForm.resetFields()
      this.treeData = []
      this.$emit('update:showDialog', false)
    },
    async ok() {
      const isValid = await this.$refs.employeeForm.validate()
      if (isValid) {
        await addEmployee(this.employeeFormData)
        //   提醒父组件关闭窗口
        this.$emit('update:showDialog', false)
        //   提醒父组件更新数据
        this.$emit('updateData')
      }
    },
    checkTwoTimeData() {
      this.$refs.employeeForm.validateField(['timeOfEntry', 'correctionTime'], this.checkTime)
    }
  }
}
</script>

<style lang="scss" scoped>
    .treeContainer{
        position: absolute;
        left:0;
        width:400px;
        height: 300px;
        border: 1px solid #ccc;
        overflow: hidden;
        z-index: 99;
        .treeDeparment{
            width:calc(100% + 17px);
            height: 100%;
            overflow-y: scroll;
        }
    }
</style>
